package common

import (
	"github.com/goravel/framework/contracts/http"
	"goravel/app/http/requests"
	"goravel/app/services"
)

type LoginController struct {
	//Dependent services
	adminService services.AdminService
}

func NewLoginController() *LoginController {
	return &LoginController{
		//Inject services
		adminService: services.AdminService{},
	}
}

func (r *LoginController) Login(ctx http.Context) http.Response {
	var request requests.LoginRequest

	validateRequest, err := ctx.Request().ValidateRequest(&request)
	if err != nil {
		return ctx.Response().Success().Json(http.Json{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    nil,
		})
	}
	if validateRequest != nil {
		return ctx.Response().Success().Json(http.Json{
			"code":    http.StatusBadRequest,
			"message": validateRequest.One(),
			"data":    nil,
		})
	}
	data, err := r.adminService.Login(ctx, request)
	if err != nil {
		return ctx.Response().Success().Json(http.Json{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    nil,
		})
	}

	return ctx.Response().Success().Json(http.Json{
		"code":    http.StatusOK,
		"message": "success",
		"data":    data,
	})

}
